var toWorkConId=null;
var isReview=null;
var isCare_allVar=null;
var isShare_allVar=null;
var isOthers_allVar=null;
var pageNo=null;
//add by jianghaibo 2015-05-22
var workConViewSearch_reviewState="";
var workConViewSearch_empName="";
var workConViewSearch_startDate="";
var workConViewSearch_endDate="";
var userPostId="";
var viewType="";
$( document ).on( "pagecreate", "#workConInfo", function( event ) {
	toWorkConId=getParameterByName("toWorkConId", getLocationUrl(event) );
	access_token=getParameterByName("access_token", getLocationUrl(event) );
	thisWorkConType = getParameterByName("workConType", getLocationUrl(event) );
	isReview=getParameterByName("isReview", getLocationUrl(event) );
	isCare_allVar=getParameterByName("isCare_allVar", getLocationUrl(event) );
	isShare_allVar=getParameterByName("isShare_allVar", getLocationUrl(event) );
	isOthers_allVar=getParameterByName("isOthers", getLocationUrl(event) );
	pageNo = getParameterByName("pageNo", getLocationUrl(event) );
	//add by jianghaibo 2015-05-22
	workConViewSearch_reviewState=getParameterByName("searchReviewState", getLocationUrl(event) );
	workConViewSearch_empName=getParameterByName("searchEmpName", getLocationUrl(event) );
	workConViewSearch_startDate=getParameterByName("searchStartDate", getLocationUrl(event) );
	workConViewSearch_endDate=getParameterByName("searchEndDate", getLocationUrl(event) );
	userPostId=getParameterByName("userPostId", getLocationUrl(event) );
	viewType=getParameterByName("viewType", getLocationUrl(event) );
	all_e=event;
	if(thisWorkConType == 3){
		workConType = '1';
	}else{
		workConType = thisWorkConType;
	}
	//以下按钮，在审阅状态下才可以看到
	if(!isNullOrEmpty(isReview) && isReview=="true"){
		if( !isNullOrEmpty(workConType) && workConType=="2"){
			$(".infoTitle").show();
		}
	}else{
		$(".infoTitle").hide();
	}
	//分享详细页面，无需评论
	if( ( !isNullOrEmpty(isShare_allVar) && isShare_allVar=="true" ) || isReview=="false" ){
		$("#workConInfo_publish").hide();
	}else{
		$("#workConInfo_publish").show();
	}
	
	if(isReview=="false"){
		//判断是否可以显示修改按钮
		loadModifyBtn();
//		$("#workConInfo_footer").show();
	}
	
	//特别关心workConInfo_specialCare isCanModifyCon
	$("#workConInfo_specialCare").on("click",function (){ 
		workConInfo_specialCare_fun(true);
	});
	//取消关心workConInfo_specialCare_no
	$("#workConInfo_specialCare_no").on("click",function (){
		workConInfo_specialCare_fun(false);
	});
	//分享workConInfo_share
	$("#workConInfo_share").on("click",function (){
		workConInfo_share_fun(true);
	});
	//取消分享workConInfo_share_no
	$("#workConInfo_share_no").on("click",function (){
		workConInfo_share_fun(false);
	});
	
	//修改  workConInfo_footer
	$("#workConInfo_modify").on("click",function (){
		goTo2(rootPath+"/jsp/weixin/workCon.jsp?access_token="+access_token+"&workConType="+thisWorkConType+"&toWorkConId="+toWorkConId+"&isModify=true"+"&pageNo="+pageNo+"&userPostId="+userPostId+"&viewType="+viewType,false);
	});
});
$( document ).on( "pageshow", "#workConInfo", function( event ) {
	
	
	workConInfo_loadReviewData();
	
	workConInfo_loadData_comments(toWorkConId);
//	$(".publish").css({"display":"block"});
//	//点击评论按钮
//	$("#workCon_addReview").on("click",function (){
//		var c = $(".publish").css("display");
//		$("[name='publishCon']").val("");
//		if("none" == c){
//			$(".publish").css({"display":"block"});
//		}else{
//			$(".publish").css({"display":"none"});
//		}
//	});
	
	// 发表评论
	$("#publishBut").on("click",function (){
		var content = $("[name='publishCon']").val();
		workConInfo_publish_comments(toWorkConId,content);
	});
	//是否显示发布按钮
	isShowPublishButton(access_token);
});
/**
 *修改按钮是否可以显示
 */
function loadModifyBtn(){
	showLoading();
	var action="/workCon/isCanModifyCon.do";
	$.ajax({
	  	url: rootPath+action,
	  	type: "POST",
	  	dataType: "text",
		data: "access_token="+access_token+"&workConType="+workConType+"&toWorkConId="+toWorkConId,
	  	success: function( response ) {
	  		var data=$.parseJSON(response);
			if(data.state=="200"){
				$("#workConInfo_footer").show();
			}
			else{
				$("#workConInfo_footer").hide();
//				alert_wx("fail",data.msg);
			}
			hideLoading();
	  	}
	});
}
/**
 * 特别关心
 */
function workConInfo_specialCare_fun(isCare){
	showLoading();
	var action="/workCon/addSpecialCare.do";
	if(isCare==false){
		action="/workCon/revomeSpecialCare.do";
	}
	$.ajax({
	  	url: rootPath+action,
	  	type: "POST",
	  	dataType: "text",
		data: "access_token="+access_token+"&workConType="+workConType+"&toWorkConId="+toWorkConId,
	  	success: function( response ) {
	  		var data=$.parseJSON(response);
			if(data.state=="200"){
				if(isCare==true){
					$("#workConInfo_specialCare").hide();
					$("#workConInfo_specialCare_no").show();
				}else{
					$("#workConInfo_specialCare").show();
					$("#workConInfo_specialCare_no").hide();
				}
			}else{
				alert_wx("fail",data.msg);
			}
			hideLoading();
	  	}
	});
}
/**
 * 分享
 */
function workConInfo_share_fun(isShare){
	showLoading();
	var action="/workCon/addWorkConShare.do";
	if(isShare==false){
		action="/workCon/removeWorkConShare.do";
	}
	$.ajax({
	  	url: rootPath+action,
	  	type: "POST",
	  	dataType: "text",
		data: "access_token="+access_token+"&workConType="+workConType+"&toWorkConId="+toWorkConId,
	  	success: function( response ) {
	  		var data=$.parseJSON(response);
			if(data.state=="200"){
				if(isShare==true){
					$("#workConInfo_share").hide();
					$("#workConInfo_share_no").show();
				}else{
					$("#workConInfo_share").show();
					$("#workConInfo_share_no").hide();
				}
			}else{
				alert_wx("fail",data.msg);
			}
			hideLoading();
	  	}
	});
}

function workConInfo_loadReviewData(){

	showLoading();
	$.ajax({
	  	url: rootPath+"/workCon/getWorkConById.do",
	  	type: "POST",
	  	dataType: "text",
		data: "access_token="+access_token+"&workConType="+workConType+"&toWorkConId="+toWorkConId,
	  	success: function( response ) {
	  		
	  		var data=$.parseJSON(response);
			if(data.state=="200"){
				var temp=$("#workConInfo_viewContent");
				setTimeout(function () { 
					
					var result=data.data;
					
//					var workCon_flag="正在等待";
//					if(result.reviewState=="2"){
//						workCon_flag="已经通过";
//					}
//					$("#workConInfo_flag").text(workCon_flag);
					
					$("#workConInfo_workTime").text(result.create_time);
					var content=result.workcon1;
//					content=content.replace(/\\n/g, "</br>");
					$("#workConInfo_viewContent").html(content+"</br>&nbsp;");
					
					var isSpecialCare=result.isSpecialCare;
					var isShare=result.isShare;
					if(!isNullOrEmpty(isSpecialCare) && isSpecialCare=="1"){
						$("#workConInfo_specialCare").hide();
						$("#workConInfo_specialCare_no").show();
					}
					if(!isNullOrEmpty(isShare) && isShare=="1"){
						$("#workConInfo_share").hide();
						$("#workConInfo_share_no").show();
					}else if(!isNullOrEmpty(isShare) && isShare=="-1"){
						$("#workConInfo_share").hide();
						$("#workConInfo_share_no").hide();
					}
					
					workCon_scrollFun();
					
					//返回列表
					$("#workConInfo_back").on('click', function () {
						userPostId=getParameterByName("userPostId", getLocationUrl(event) );
						viewType =  getParameterByName("viewType", getLocationUrl(event) );
						if(!isNullOrEmpty(isReview) && isReview=="true"){
							goTo2(rootPath+"/jsp/weixin/workConViewList.jsp?access_token="+access_token+"&workConType="+thisWorkConType+"&pageNo="+pageNo
									+"&searchReviewState="+workConViewSearch_reviewState+"&searchEmpName="+workConViewSearch_empName 
									+"&searchStartDate="+workConViewSearch_startDate+"&searchEndDate="+workConViewSearch_endDate+"&userPostId="+userPostId+"&viewType="+viewType,false);
						}else if(!isNullOrEmpty(isCare_allVar) && isCare_allVar=="true"){
							goTo2(rootPath+"/jsp/weixin/workConCareList.jsp?access_token="+access_token+"&workConType="+thisWorkConType+"&pageNo="+pageNo+"&userPostId="+userPostId+"&viewType="+viewType,false);
						}else if(!isNullOrEmpty(isShare_allVar) && isShare_allVar=="true"){
							goTo2(rootPath+"/jsp/weixin/workConShareList.jsp?access_token="+access_token+"&workConType="+thisWorkConType+"&pageNo="+pageNo+"&userPostId="+userPostId+"&viewType="+viewType,false);
						}else if(!isNullOrEmpty(isOthers_allVar)){
							goTo2(rootPath+"/jsp/weixin/workConListOfOthers.jsp?access_token="+access_token+"&workConType="+thisWorkConType+"&toWorkConId="+toWorkConId+"&fromPage="+isOthers_allVar+"&pageNo="+pageNo+"&userPostId="+userPostId+"&viewType="+viewType,false);
						}else{
							goTo2(rootPath+"/jsp/weixin/workConList.jsp?access_token="+access_token+"&workConType="+thisWorkConType+"&pageNo="+pageNo+"&userPostId="+userPostId+"&viewType="+viewType,false);
							
						}
					});
					
					hideLoading();
				}, 800);
				
			}else{
				alert_wx("fail",data.msg);
				hideLoading();
			}
	  	}
	});
}
var workCon_myScroll=null;
var workCon_myScroll2=null;
function workCon_scrollFun() {
	if(workCon_myScroll!=null){
		workCon_myScroll.destroy();
	}
	workCon_myScroll = new iScroll('wrapper_viewContent', {
		scrollbarClass: 'myScrollbar'
	});
	if(workCon_myScroll2 !=null){
		workCon_myScroll2.destroy();
	}
	workCon_myScroll2 = new iScroll('wrapper_viewReviewContent', {
		scrollbarClass: 'myScrollbar'
	});
}
//加载评论列表
function workConInfo_loadData_comments(toWorkConId){
	$.ajax({
	  	url: rootPath+"/workCon/workReviewList.do",
	  	type: "POST",
	  	dataType: "text",
		data: "access_token="+access_token+"&workConType="+workConType+"&toWorkConId="+toWorkConId,
	  	success: function( response ) {
	  		var data=$.parseJSON(response);
			if(data.state=="200"){
				var content = "";
				var arr=data.data;
				for (var i=0;i<arr.length;i++){
//					content+="<li>"+arr[i].empName+"：<span>"+arr[i].comCon+"</span></li>";
					content +='<li data-icon="false">\
						  <a href="#" style="padding:2px">\
							<p ><span >'+arr[i].empName+'('+arr[i].posiName+')</span><span class="ui-li-aside_fresh" style="top: 2px;right: 10px;">'+arr[i].create_time+'</span></p>\
							<p  name=\'whiteSpace'+pageNo+'\'><span >'+arr[i].comCon+'</span></p>\
						  </a>\
						</li>';
				}
				$("#thelist_workConInfo").html(content).listview('refresh');
				$("[name='whiteSpace"+pageNo+"']").on("tap",function (){
					p_show(this);
				});
			}else{
				alert_wx('fail',data.msg);
			}
	  	}
	});
}
//发表评论
function workConInfo_publish_comments(toWorkConId,content){
	if(isNullOrEmpty(toWorkConId)){
		alert_wx('fail',"页面错误，请刷新页面！");
		return false;
	}
	if(isNullOrEmpty(content)){
		alert_wx('fail',"评论内容不能为空！");
		return false;
	}else if(content.length>100){
		alert_wx('fail',"评论字数不能多于100！");
		return false;
	}
	$.ajax({
	  	url: rootPath+"/workCon/addworkConReview.do",
	  	type: "POST",
	  	dataType: "text",
		data: "access_token="+access_token+"&workConType="+workConType+"&toWorkConId="+toWorkConId+"&content="+content,
	  	success: function( response ) {
	  		var data=$.parseJSON(response);
			if(data.state=="200"){
//				alert_wx('success',"发表成功！");
//				$(".publish").hide();
				$("[name='publishCon']").val("");
				workConInfo_loadData_comments(toWorkConId);
			}else{
				alert_wx('fail',data.msg);
			}
	  	}
	});
}

function isShowPublishButton(access_token){
	$.ajax({
	  	url: rootPath+"/workCon/isBranchManager.do",
	  	type: "POST",
	  	dataType: "text",
		data: "access_token="+access_token,
	  	success: function( response ) {
	  		var data=$.parseJSON(response);
	  		//如果为分公司管理员无评论权限
			if(data.state=="200"){
				$("#workConInfo_publish").hide();
				alert_wx('fail',data.msg);
			}else{
				$("#workConInfo_publish").show();
			}
	  	}
	});
}